#include "asm_utils.h"
#include "interrupt.h"
#include "stdio.h"
#include "program.h"
#include "thread.h"

// 屏幕IO处理器
STDIO stdio;
// 中断管理器
InterruptManager interruptManager;
// 程序管理器
ProgramManager programManager;

void first_thread(void *arg)
{
    printf("pid %d name \"%s\"\n", programManager.running->pid, programManager.running->name);
    while(1);
}

void second_thread(void *arg)
{
    printf("pid %d name \"%s\"\n", programManager.running->pid, programManager.running->name);
}

void third_thread(void *arg)
{
    printf("pid %d name \"%s\"\n", programManager.running->pid, programManager.running->name);
}

void fourth_thread(void *arg)
{
    printf("pid %d name \"%s\"\n", programManager.running->pid, programManager.running->name);
}
extern "C" void setup_kernel()
{

    // 中断管理器
    interruptManager.initialize();
    interruptManager.enableTimeInterrupt();
    interruptManager.setTimeInterrupt((void *)asm_time_interrupt_handler);

    // 输出管理器
    stdio.initialize();

    // 进程/线程管理器
    programManager.initialize();

    // 创建第一个线程
    int pid = programManager.executeThread(first_thread, nullptr, "first thread", 4);
    programManager.executeThread(second_thread, nullptr, "second thread", 2);
    programManager.executeThread(third_thread, nullptr, "third thread", 1);
    programManager.executeThread(fourth_thread, nullptr, "fourth thread", 3);
    ListItem *item = programManager.readyPrograms.front();
    PCB *firstThread = ListItem2PCB(item, tagInGeneralList);
    firstThread->status = RUNNING;
    programManager.readyPrograms.pop_front();
    programManager.running = firstThread;
    asm_switch_thread(0, firstThread);

    asm_halt();
}
